package utils;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class FileReaders {


    private String filepath;
    private BufferedReader bufferedReader;

    private Integer batchNum = 1000;

    public FileReaders(String filepath) throws FileNotFoundException {
        this.filepath = filepath;
        bufferedReader = new BufferedReader(new FileReader(filepath));
    }

    public FileReaders(String filepath, Integer batchNum) throws FileNotFoundException {
        this.filepath = filepath;
        this.batchNum = batchNum;
        bufferedReader = new BufferedReader(new FileReader(filepath));
    }

    public List<String> reabBatch() throws IOException {

        ArrayList<String> list = new ArrayList<>();

        int count = 0;

        while ( true ){
            String line = bufferedReader.readLine();
            if (line!=null){
                list.add(line);
                count++;
                if (count == batchNum){ // 如果已经读够了 批次数量，则直接把list返回
                    return list;
                }
            }else {  // 如果读到文件末尾了，则直接返回list
                return list;
            }
        }

    }

    public void close() throws IOException {
        this.bufferedReader.close();
    }

    public static void main(String[] args) throws IOException {
        FileReaders fileReaders = new FileReaders("D:\\aaa.txt", 10);
        while (true){
            //读取文件
            List<String> list = fileReaders.reabBatch();
            if (list.size()==0){
                System.out.println("finish");
                return;
            }
            for (String s : list) {
                System.out.println(s);
            }
            System.out.println("=========");

        }
    }
}
